package com.pgytesting.secondhandcar.service.impl;

import com.github.pagehelper.util.StringUtil;
import com.pgytesting.secondhandcar.dao.OrderNameDao;
import com.pgytesting.secondhandcar.dao.SalesCommissionSettlementDao;
import com.pgytesting.secondhandcar.dao.VehicleInformationDao;
import com.pgytesting.secondhandcar.model.OrderName;
import com.pgytesting.secondhandcar.model.SalesCommissionSettlement;
import com.pgytesting.secondhandcar.service.SalesCommissionSettlementService;
import com.pgytesting.secondhandcar.util.SessionUtil;
import com.pgytesting.secondhandcar.util.UtilMethod;
import com.pgytesting.secondhandcar.vo.ResponseMessage;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author : frosty0804@gmail.com
 * @version : v1.0
 * @projectName : secondhandcar
 * @className : SalesCommissionSettlementServiceImpl
 * @description : TODO(一句话描述该类的功能)
 * @date : 2019/8/29 19:39
 */
@Service("SalesCommissionSettlementService")
public class SalesCommissionSettlementServiceImpl implements SalesCommissionSettlementService {

    @Resource
    private SalesCommissionSettlementDao salesCommissionSettlementDao;

    @Resource
    private OrderNameDao orderNameDao;

    @Resource
    private VehicleInformationDao vehicleInformationDao;


    /**
     * 插入数据
     *
     * @param salesCommissionSettlement
     * @return
     */
    @Override
    public int insert(SalesCommissionSettlement salesCommissionSettlement) {
        return salesCommissionSettlementDao.insert(salesCommissionSettlement);
    }

    /**
     * 查询全部数据
     *
     * @return
     */
    @Override
    public ResponseMessage selectAll() {
        // 获取登陆用户ID
        String mcpCarUserNumber = SessionUtil.getSessionMcpCar().getUserNumber();
        // 进行数据查询
        List<SalesCommissionSettlement> resultList = salesCommissionSettlementDao.queryAll(mcpCarUserNumber);
        // 进行参数计算
        // 累计收益
        Double income = 0.00;
        // 待结算
        Double notSettlement = 0.00;
        // 人数
        Set<String> userNumber = new HashSet();
        for (SalesCommissionSettlement settlement : resultList) {
            if ("1".equals(settlement.getSettlementStatus())) {
                income += settlement.getAmountIncome();
            } else {
                notSettlement += settlement.getAmountIncome();
            }
            userNumber.add(settlement.getOrderNameId());
        }
        // 拼接SQL进行数据查询
        StringBuilder sf = new StringBuilder();
        Integer count = 0;
        for (String s : userNumber) {
            if (count == 0) {
                sf.append("'" + s + "'");
            } else {
                sf.append(",'" + s + "'");
            }
            count++;
        }
        Integer countUser = 0;
        if (StringUtil.isNotEmpty(sf.toString())) {
            countUser = orderNameDao.queryCountUser(sf.toString());
        }
        // 利用时间戳计算当天新增订单为新增客户
        List<OrderName> monthOrderName = orderNameDao.queryMonthOrder(UtilMethod.startMonth());
        // 创建遍历迭代结果
        Set<String> monthSet = new HashSet<>();
        monthOrderName.forEach(o -> {
            monthSet.add(o.getId());
        });
        // 利用时间戳计算当天修改时间来确定今日订单
        List<OrderName> dayOrderName = orderNameDao.queryMonthOrderCount(UtilMethod.getTodayZero());
        count = 0;
        StringBuilder sf1 = new StringBuilder();
        for (OrderName o : dayOrderName) {
            if (count == 0) {
                sf1.append("'" + o.getVehicleInformationId() + "'");
            } else {
                sf1.append(",'" + o.getVehicleInformationId() + "'");
            }
            count++;
        }
        // 查询申请车款
        Double totalOrderToday = 0.00;
        if (StringUtil.isNotEmpty(sf1.toString())){
            totalOrderToday = vehicleInformationDao.queryCountPrice(String.valueOf(sf1));
        }
        // 计算今日订单金额
        count = 0;
        StringBuilder sf2 = new StringBuilder();
        for (OrderName o : monthOrderName) {
            if (count == 0) {
                sf2.append("'" + o.getVehicleInformationId() + "'");
            } else {
                sf2.append(",'" + o.getVehicleInformationId() + "'");
            }
            count++;
        }
        Double newOrderTotal = 0.00;
        if (StringUtil.isNotEmpty(sf2.toString())){
            newOrderTotal = vehicleInformationDao.queryCountPrice(sf2.toString());
        }

        // 计算今日收益
        Double todayIncome = orderNameDao.todayIncome(UtilMethod.getTodayZero());
        Map<String, Object> resultMap = new HashMap<>();
        // 累计收益
        resultMap.put("income", income);
        // 待结算收益
        resultMap.put("notSettlement", notSettlement);
        // 累计客户
        resultMap.put("countUser", countUser);
        // 累计订单
        resultMap.put("cumulativeOrder", userNumber.size());
        // 当月累计订单，需要传递订单ID,订单详情
        resultMap.put("cumulativeOrderMonth", monthSet);
        // 今日收益
        resultMap.put("todayIncome", todayIncome);
        // 今日订单总额
        resultMap.put("totalOrderToday", totalOrderToday);
        // 今日新增客户订单金额
        resultMap.put("newOrderTotal", newOrderTotal);
        return ResponseMessage.successResponse(resultMap, "查询成功");
    }


    /**
     * 更新数据
     *
     * @param salesCommissionSettlement
     * @return
     */
    @Override
    public int update(SalesCommissionSettlement salesCommissionSettlement) {
        return salesCommissionSettlementDao.update(salesCommissionSettlement);
    }
}
